//
//  PKNavgatinBarConfigurable.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/14.
//

import UIKit

protocol NavigationBarConfigurable where Self: UIViewController {
    var navBarTitle: String? { get }
    var prefersLargeTitle: Bool { get }
    var showBackButton: Bool { get }
    var shouldHideNavigationBar: Bool { get }
    var rightBarButton: NavigationBarButton? { get }
    func setupNavigationBar()
    func updateNavBarVisibility(animated: Bool)
    var customBackButton: NavigationBarButton? { get }
}

struct NavigationBarButton {
    var title: String?
    var image: UIImage?
    var action: Selector
}

extension NavigationBarConfigurable {
    //    var navBarTitle: String? { nil }
    //    var rightBarButton: NavigationBarButton? { nil }
    //    var prefersLargeTitle: Bool { false }
    //    var shouldHideNavigationBar: Bool { true }
    var navBarTitle: String? { nil }
    var prefersLargeTitle: Bool { false }
    var showBackButton: Bool { true }
    var shouldHideNavigationBar: Bool { false }
    var rightBarButton: NavigationBarButton? { nil }
    var customBackButton: NavigationBarButton? { nil }
    func setupNavigationBar() {
        guard let nav = navigationController else { return }
        
        nav.setNavigationBarHidden(shouldHideNavigationBar, animated: false)
        if shouldHideNavigationBar { return }
        
        nav.navigationBar.prefersLargeTitles = prefersLargeTitle
        nav.navigationBar.isTranslucent = false
        nav.navigationBar.barTintColor = .white
        nav.navigationBar.titleTextAttributes = [
            .foregroundColor: UIColor.black,
            .font: UIFont.boldSystemFont(ofSize: 18)
        ]
        title = navBarTitle
        
        if let right = rightBarButton {
            let button = UIBarButtonItem(title: right.title,
                                         style: .plain,
                                         target: self,
                                         action: right.action)
            button.image = right.image
            navigationItem.rightBarButtonItem = button
        }
        
        
        
        if showBackButton, nav.viewControllers.count > 1 {
            if let custom = customBackButton {
                let backItem = UIBarButtonItem(
                    title: custom.title,
                    style: .plain,
                    target: self,
                    action: custom.action
                )
                backItem.image = custom.image
                backItem.tintColor = .white
                navigationItem.leftBarButtonItem = backItem
            } else {
                let backItem = UIBarButtonItem(
                    image: UIImage(systemName: "chevron.left"),
                    style: .plain,
                    target: self,
                    action: #selector(backAction)
                )
                backItem.tintColor = .white
                navigationItem.leftBarButtonItem = backItem
            }
        }
    }
}

extension UIViewController {
    @objc func backAction() {
        navigationController?.popViewController(animated: true)
    }
}
extension NavigationBarConfigurable {
    func updateNavBarVisibility(animated: Bool = true) {
        navigationController?.setNavigationBarHidden(shouldHideNavigationBar, animated: animated)
    }
}
